package com.hdjk.view.callback;

import com.hdjk.view.entity.GroupEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class GroupDetailCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject object = (JSONObject) msg.getData();
        if (object!=null) {
            GroupEntity entity = new GroupEntity();
            if (!object.isNull("portfolioCode")) {
                entity.setPortfolioCode(object.getString("portfolioCode"));
            }
            if (!object.isNull("portfolioName")) {
                entity.setPortfolioName(object.getString("portfolioName"));
            }
            if (!object.isNull("day1Performance")) {
                double l = object.getDouble("day1Performance");
                BigDecimal b = new BigDecimal(l);
                entity.setDay1Performance(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            }
            if (!object.isNull("minBuyAmount")) {
                double l = object.getDouble("minBuyAmount");
                BigDecimal b = new BigDecimal(l);
                entity.setMinBuyAmount(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            }
            if (!object.isNull("performance")) {
                JSONObject performance = object.getJSONObject("performance");
                GroupEntity.Performance per = entity.new Performance();
                if (!performance.isNull("performanceYear1")) {
                    double l = performance.getDouble("performanceYear1");
                    BigDecimal b = new BigDecimal(l);
                    per.setPerformanceYear1(b.setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                }
                entity.setPerformance(per);
            }
            if (!object.isNull("portfolioStrategy")) {
                entity.setPortfolioStrategy(object.getString("portfolioStrategy"));
            }
            if (!object.isNull("portfolioFund")) {
                JSONArray funds = object.getJSONArray("portfolioFund");
                List<GroupEntity.PortfolioFund> list = new ArrayList<>();
                for (int j = 0; j < funds.length(); j++) {
                    JSONObject fundObject = funds.getJSONObject(j);
                    GroupEntity.PortfolioFund fund = entity.new PortfolioFund();
                    if (!fundObject.isNull("fundCode")) {
                        fund.setFundCode(fundObject.getString("fundCode"));
                    }
                    if (!fundObject.isNull("fundName")) {
                        fund.setFundName(fundObject.getString("fundName"));
                    }
                    if (!fundObject.isNull("fundType")) {
                        fund.setFundType(fundObject.getString("fundType"));
                    }
                    if (!fundObject.isNull("proportion")) {
                        double l = fundObject.getDouble("proportion");
                        BigDecimal b = new BigDecimal(l);
                        fund.setProportion(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    list.add(fund);
                }
                if (!object.isNull("portfolioRisk")) {
                    entity.setPortfolioRisk(object.getString("portfolioRisk"));
                }
                entity.setPortfolioFund(list);
            }
            msg.setData(entity);
        }
        return msg;
    }
}
